package com.zhilianbao.leyaogo.ui.adapter.shoppingcart;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.bql.animationcheckbox.AnimationCheckBox;
import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.bql.baseadapter.recycleView.QuickRcvHolder;
import com.bql.utils.DateUtils;
import com.bql.utils.NumberUtlis;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.Coupon.CanUseCoupon;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Author: hcy
 * Description:
 * Created: 2016/11/29 20:00
 */
public class CouponCanUseAdapter extends QuickRcvAdapter<CanUseCoupon> {

    private int selectPos = -1;
    private AnimationCheckBox mSelectCheckBox;

    public CouponCanUseAdapter(Context context, List<CanUseCoupon> data,int pos) {
        super(context, data, R.layout.item_coupon_can_use);
        selectPos = pos;
    }

    @Override
    protected void bindDataHelper(QuickRcvHolder viewHolder, int position, CanUseCoupon item) {
        boolean isCash = item.getCouponsType() == 0;//现金券
        MoneyTextView mtv = viewHolder.getView(R.id.mtv);
        if(isCash){
            mtv.setAmount(String.valueOf(item.getCouponsVal()));
            mtv.setSymbolTextSize(Utils.sp2px(12));
            viewHolder.getView(R.id.tv_discount).setVisibility(View.GONE);
        }else {
            double value = NumberUtlis.mul(item.getCouponsVal(),0.1);
            mtv.setDecimalFormat(new DecimalFormat("####0.0"));
            mtv.setAmount(value);
            mtv.setSymbolTextSize(0);
            viewHolder.getView(R.id.tv_discount).setVisibility(View.VISIBLE);
        }

        String limit = "";
        if(item.getCouponsLimitPrice()==0.0f){
            limit = mContext.getString(R.string.no_limit);
        }else{
            limit = mContext.getString(R.string.enough)+item.getCouponsLimitPrice()+mContext.getString(R.string.use_money);
        }

        String limitTime = "";
        if (item.getDayLimit()==0) {// 日期约束方式 0 日期段约束 1 日期天数约束 ,
            limitTime = mContext.getString(R.string.end_day)+ DateUtils.format(Long.parseLong(item.getEndDay()),DateUtils.DATE_FORMAT_STD);
        }else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.parseLong(item.getUserGetDtm()));
            calendar.add(Calendar.DAY_OF_YEAR,item.getDayNumber());
            limitTime = mContext.getString(R.string.end_day)+ DateUtils.format(calendar.getTimeInMillis(),DateUtils.DATE_FORMAT_STD);
        }
        viewHolder.setText(R.id.tv_type,isCash ? mContext.getString(R.string.cash_ticket) : mContext.getString(R.string.discount_ticket))
                    .setText(R.id.tv_limit,limit).setText(R.id.tv_limit_time,limitTime);
        FrameLayout fl = viewHolder.getView(R.id.fl_bg);
        int resId = isCash ? R.drawable.bg_cash_coupon : R.drawable.bg_discount_coupon;
        fl.setBackgroundResource(resId);


        AnimationCheckBox checkBox = viewHolder.getView(R.id.checkbox);
        if(item.isCheck()){
            checkBox.setChecked(true,false,false);
            selectPos = position;
            mSelectCheckBox = checkBox;
        }else {
            checkBox.setChecked(false,false,false);
        }
        checkBox.setTag(position);
        checkBox.setOnCheckedChangeListener(new AnimationCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(AnimationCheckBox checkBox, boolean isChecked) {
                int pos = (int) checkBox.getTag();
                if(isChecked){
                    if(mSelectCheckBox!=null){
                        mSelectCheckBox.setChecked(false,false);
                    }
                    mSelectCheckBox = checkBox;
                    selectPos = pos;
                    mList.get(pos).setCheck(true);
                }else {
                    if(mSelectCheckBox!=null&&checkBox==mSelectCheckBox){
                        mSelectCheckBox = null;
                        selectPos = -1;
                    }
                    mList.get(pos).setCheck(false);
                }
                if(mOnCheckChangeListener!=null){
                    mOnCheckChangeListener.checkChange(selectPos);
                }
            }
        });
    }

    public interface OnCheckChangeListener{
        void checkChange(int pos);
    }

    private  OnCheckChangeListener mOnCheckChangeListener;

    public void setOnCheckChangeListener(OnCheckChangeListener listener){
        mOnCheckChangeListener = listener;
    }
}
